package com.qut.graduate.busrtquery.greendao.controller;

import android.content.Context;

import com.qut.graduate.busrtquery.BRTQCache;
import com.qut.graduate.busrtquery.BRTQConstant;
import com.qut.graduate.busrtquery.greendao.bean.BusLine;
import com.qut.graduate.busrtquery.greendao.bean.BusLineCollect;
import com.qut.graduate.busrtquery.greendao.bean.StationInfo;
import com.qut.graduate.busrtquery.greendao.bean.StationInfoCollect;
import com.qut.graduate.busrtquery.greendao.dao.BusLineCollectDao;
import com.qut.graduate.busrtquery.greendao.dao.BusLineDao;
import com.qut.graduate.busrtquery.greendao.dao.StationInfoCollectDao;
import com.qut.graduate.busrtquery.greendao.dao.StationInfoDao;
import com.qut.graduate.busrtquery.utils.common.LogUtils;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author WLL
 * @Time: 2019/3/26 18:48
 * 数据库工具类
 */
public class DBController {

    private GreenDaoController mDaoController;
    private WeakReference<Context> mWeakReference;
    private static DBController mDBController;

    public static DBController getInstance() {
        if (null == mDBController) {
            synchronized (DBController.class) {
                if (null == mDBController) {
                    mDBController = new DBController();
                }
            }
        }
        return mDBController;
    }

    /**
     * @param context
     */
    public void init(Context context) {
        mWeakReference = new WeakReference<>(context);
        if (null == mDaoController) mDaoController = new GreenDaoController(mWeakReference.get());
        List<BusLine> busLines = (ArrayList<BusLine>) queryTable(BRTQConstant.DB_TABLE_TYPE_BUS_LINE_HISTORY);
        List<BusLineCollect> busLineCollects = (ArrayList<BusLineCollect>) queryTable(BRTQConstant.DB_TABLE_TYPE_BUS_LINE_COLLECT);
        List<StationInfo> stationInfos = (ArrayList<StationInfo>) queryTable(BRTQConstant.DB_TABLE_TYPE_STATION_INFO_HISTORY);
        List<StationInfoCollect> stationInfoCollects = (ArrayList<StationInfoCollect>) queryTable(BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT);
        BRTQCache.getInstance().setBusLineHistoryList(busLines);
        BRTQCache.getInstance().setBusLineCollectList(busLineCollects);
        BRTQCache.getInstance().setStationInfoHistoryList(stationInfos);
        BRTQCache.getInstance().setStationInfoCollectList(stationInfoCollects);

        LogUtils.d("----数据库查询 BusLineHistoryList：" + busLines);
        LogUtils.d("----数据库查询 BusLineCollectList：" + busLineCollects);
        LogUtils.d("----数据库查询 StationInfoHistoryList：" + stationInfos);
        LogUtils.d("----数据库查询 StationInfoCollectList：" + stationInfoCollects);
    }

    /**
     * 获取Dao控制类
     *
     * @param daoType
     * @return
     */
    private Object getDaoController(int daoType) {
        switch (daoType) {
            case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_HISTORY:
                return mDaoController.getDaoSession().getBusLineDao();
            case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_HISTORY:
                return mDaoController.getDaoSession().getStationInfoDao();
            case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_COLLECT:
                return mDaoController.getDaoSession().getBusLineCollectDao();
            case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT:
                return mDaoController.getDaoSession().getStationInfoCollectDao();
            default:
                return null;
        }
    }

    /**
     * 查询表中所有
     *
     * @param type
     * @return
     */
    public ArrayList<?> queryTable(int type) {
        try {
            switch (type) {
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_HISTORY:
                    return (ArrayList<?>) ((BusLineDao) getDaoController(type)).loadAll();
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_HISTORY:
                    return (ArrayList<?>) ((StationInfoDao) getDaoController(type)).loadAll();
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_COLLECT:
                    return (ArrayList<?>) ((BusLineCollectDao) getDaoController(type)).loadAll();
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT:
                    return (ArrayList<?>) ((StationInfoCollectDao) getDaoController(type)).loadAll();
                default:
                    return null;
            }
        } catch (Exception e) {
            LogUtils.d("E-数据库查询异常：" + e);
            return null;
        }
    }

    /**
     * 根据name查询Busline
     *
     * @param busName
     * @return
     */
    public ArrayList<?> queryObjectByName(int type, String busName) {
        try {
            switch (type) {
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_HISTORY:
                    List<BusLine> busLineList = ((BusLineDao) getDaoController(type)).queryBuilder()
                            .where(BusLineDao.Properties.Name.eq(busName)).build().list();
                    return (ArrayList<?>) busLineList;
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_COLLECT:
                    List<BusLineCollect> busLineCollectList = ((BusLineCollectDao) getDaoController(type)).queryBuilder()
                            .where(BusLineCollectDao.Properties.Name.eq(busName)).build().list();
                    return (ArrayList<?>) busLineCollectList;
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_HISTORY:
                    List<StationInfo> stationInfoList = ((StationInfoDao) getDaoController(type)).queryBuilder()
                            .where(StationInfoDao.Properties.Name.eq(busName)).build().list();
                    return (ArrayList<?>) stationInfoList;
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT:
                    List<StationInfoCollect> stationInfoCollectList = ((StationInfoCollectDao) getDaoController(type)).queryBuilder()
                            .where(StationInfoCollectDao.Properties.Name.eq(busName)).build().list();
                    return (ArrayList<?>) stationInfoCollectList;
                default:
                    return null;
            }
        } catch (Exception e) {
            LogUtils.d("E-数据库根据name查询Busline异常：" + e);
            return null;
        }
    }

    /**
     * 单条插入表格
     * 不必担心空指针异常，因为找Dao的case是一样的
     *
     * @param type
     * @param object
     * @return
     */
    public boolean insertTable(int type, Object object) {
        try {
            switch (type) {
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_HISTORY:
                    return object instanceof BusLine && ((BusLineDao) getDaoController(type)).insert((BusLine) object) != -1;
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_HISTORY:
                    return object instanceof StationInfo && ((StationInfoDao) getDaoController(type)).insert((StationInfo) object) != -1;
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_COLLECT:
                    return object instanceof BusLineCollect && ((BusLineCollectDao) getDaoController(type)).insert((BusLineCollect) object) != -1;
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT:
                    return object instanceof StationInfoCollect && ((StationInfoCollectDao) getDaoController(type)).insert((StationInfoCollect) object) != -1;
                    default:
                    return false;
            }
        } catch (Exception e) {
            LogUtils.d("E-数据库插入异常：" + e);
            return false;
        }
    }

    /**
     * 单条插入表格
     * 不必担心空指针异常，因为找Dao的case是一样的
     *
     * @param type
     * @param objects
     * @return
     */
    public void insertInTxTable(int type, List<?> objects) {
        try {
            switch (type) {
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_HISTORY:
                    ((BusLineDao) getDaoController(type)).insertInTx((List<BusLine>) objects);
                    break;
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_HISTORY:
                    ((StationInfoDao) getDaoController(type)).insertInTx((List<StationInfo>) objects);
                    break;
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_COLLECT:
                    ((BusLineCollectDao) getDaoController(type)).insertInTx((List<BusLineCollect>) objects);
                    break;
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT:
                    ((StationInfoCollectDao) getDaoController(type)).insertInTx((List<StationInfoCollect>) objects);
                    break;
                default:

                    break;
            }
        } catch (Exception e) {
            LogUtils.d("E-数据库插入异常：" + e);
        }
    }

    /**
     * 单条更新表格
     * 不必担心空指针异常，因为找Dao的case是一样的
     *
     * @param type
     * @param object
     * @return
     */
    public boolean updateTable(int type, Object object) {
        try {
            switch (type) {
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_HISTORY:
                    if (!(object instanceof BusLine)) return false;
                    ((BusLineDao) getDaoController(type)).save((BusLine) object);
                    return true;
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_HISTORY:
                    if (!(object instanceof StationInfo)) return false;
                    ((StationInfoDao) getDaoController(type)).save((StationInfo) object);
                    return true;
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_COLLECT:
                    if (!(object instanceof BusLineCollect)) return false;
                    ((BusLineCollectDao) getDaoController(type)).save((BusLineCollect) object);
                    return true;
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT:
                    if (!(object instanceof StationInfoCollect)) return false;
                    ((StationInfoCollectDao) getDaoController(type)).save((StationInfoCollect) object);
                    return true;
                default:
                    return false;
            }
        } catch (Exception e) {
            LogUtils.d("E-数据库更新异常：" + e);
            return false;
        }
    }

    /**
     * 单条删除表格
     * 不必担心空指针异常，因为找Dao的case是一样的
     *
     * @param type
     * @param object
     * @return
     */
    public boolean deleteTable(int type, Object object) {
        try {
            switch (type) {
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_HISTORY:
                    if (!(object instanceof BusLine)) return false;
                    ((BusLineDao) getDaoController(type)).delete((BusLine) object);
                    return true;
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_HISTORY:
                    if (!(object instanceof StationInfo)) return false;
                    ((StationInfoDao) getDaoController(type)).delete((StationInfo) object);
                    return true;
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_COLLECT:
                    if (!(object instanceof BusLineCollect)) return false;
                    ((BusLineCollectDao) getDaoController(type)).delete((BusLineCollect) object);
                    return true;
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT:
                    if (!(object instanceof StationInfoCollect)) return false;
                    ((StationInfoCollectDao) getDaoController(type)).delete((StationInfoCollect) object);
                    return true;
                default:
                    return false;
            }
        } catch (Exception e) {
            LogUtils.d("E-数据库删除异常：" + e);
            return false;
        }
    }

    /**
     * 从收藏表中删除不是当前用户的数据
     *
     * @param type
     * @param userId
     * @return
     */
    public void deleteNotThisUserCollect(int type,String userId) {
        try {
            switch (type) {
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_COLLECT:
                    List<BusLineCollect> deleteBusLineList = ((BusLineCollectDao) getDaoController(BRTQConstant.DB_TABLE_TYPE_BUS_LINE_COLLECT))
                            .queryBuilder().where(BusLineCollectDao.Properties.UserId.eq(userId)).build().list();
                    if (null != deleteBusLineList) {
                        ((BusLineCollectDao) getDaoController(BRTQConstant.DB_TABLE_TYPE_BUS_LINE_COLLECT)).deleteInTx(deleteBusLineList);
                    }
                    break;
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT:
                    List<StationInfoCollect> deleteStationInfoList = ((StationInfoCollectDao) getDaoController(BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT))
                            .queryBuilder().where(StationInfoCollectDao.Properties.UserId.eq(userId)).build().list();
                    if (null != deleteStationInfoList) {
                        ((StationInfoCollectDao) getDaoController(BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT)).deleteInTx(deleteStationInfoList);
                    }
                    break;
            }
        } catch (Exception e) {
            LogUtils.d("E-数据库删除指定天数前数据异常："+ e);
        }
    }

    /**
     * 删除表内所有数据
     * 不必担心空指针异常，因为找Dao的case是一样的
     *
     * @param type
     */
    public void deleteAllTable(int type) {
        try {
            switch (type) {
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_HISTORY:
                    ((BusLineDao) getDaoController(type)).deleteAll();
                    break;
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_HISTORY:
                    ((StationInfoDao) getDaoController(type)).deleteAll();
                    break;
                case BRTQConstant.DB_TABLE_TYPE_BUS_LINE_COLLECT:
                    ((BusLineCollectDao) getDaoController(type)).deleteAll();
                    break;
                case BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT:
                    ((StationInfoCollectDao) getDaoController(type)).deleteAll();
                    break;
                default:
                    return;
            }
        } catch (Exception e) {
            LogUtils.d("E-数据库删除全部数据异常：" + e);
        }
    }

}
